package com.jiangdong.craft.controller;

import com.jiangdong.craft.dto.RequestDTO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@CrossOrigin
public class TestController {

    @GetMapping("/request.do")
    public String test(@RequestBody RequestDTO requestDTO) {
        StringBuffer attributeSql = new StringBuffer(" @> '[");
        List<Map<String, Object>> list = requestDTO.getAttribute();
        for (Map<String, Object> map : list) {
            attributeSql.append("{");
            for (Map.Entry entry : map.entrySet()) {
                attributeSql.append("\"").append(entry.getKey()).append("\"").append(":");
                if (isNumber(entry.getValue().toString())) {
                    attributeSql.append(entry.getValue()).append(",");
                } else {
                    attributeSql.append("\"").append(entry.getValue()).append("\"").append(",");
                }
            }
            attributeSql.deleteCharAt(attributeSql.length() - 1);
            attributeSql.append("},");
        }
        attributeSql.deleteCharAt(attributeSql.length() - 1);
        attributeSql.append("]'").append(" ::jsonb");
        System.out.println(attributeSql.toString());
        System.out.println(attributeSql);
        return "success";
    }

    public static boolean isNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57) {
                return false;
            }
        }
        return true;
    }

}
